<?php
declare (strict_types = 1);
namespace app\api\controller;
use app\api\BaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\facade\Request;
use zjkal\TimeHelper;//调用时间库
class Kuaidi extends BaseController
{
	public function index(){
	 if(request()->isPost()){
		$id=request()->param('id');
		$userid=request()->param('userid');
		$order=Db::name('orderlist')->where(['id'=>$id])->find();
		$userinfo=Db::name('useraddress')->where(['user_id'=>$userid,'isDefault'=>1])->find();
		// 截取最后四位收货人手机号
		$lastFour = substr($userinfo['phone'], -4);
		if(empty($order['deliverorder'])){
			return json(['code'=>404,'msg'=>'订单号不存在!']);  
		}
		$data=getOrderTracesByJson($order['deliverorder'],$lastFour);
		$data=json_decode($data,true);
		// print_r($data['Traces']);
		    return json(['code'=>200,'msg'=>'获取成功','data'=>$data]);  
		}else{
			return json(['code'=>404,'msg'=>'请求方式错误']);
		}
	}
	// 确认是否收货
	public function getorder(){
		 if(request()->isPost()){
			 $id=request()->param('id');
			 $order=Db::name('orderlist')->where(['id'=>$id])->find();
			 $userid=request()->param('userid');
			 $userinfo=Db::name('useraddress')->where(['user_id'=>$userid,'isDefault'=>1])->find();
			 $lastFour = substr($userinfo['phone'], -4);
			 $data=getOrderTracesByJson($order['deliverorder'],$lastFour);
			 $data=json_decode($data,true);
			 if($data['State']==3){
				 // 签收的第二天开始算起
				 $time=TimeHelper::afterDay(1);
				 // 获取指定时间几天后的时间戳
				 $endtime=TimeHelper::afterDay(intval($order['lease']),$time);
				 Db::name('orderlist')->where(['id'=>$id])->update(['state'=>3,'start_time'=>$time,'sign_time'=>time(),'end_time'=>$endtime]);
				 return json(['code'=>200,'msg'=>'确认收货成功']);  
			 }else{
				 return json(['code'=>404,'msg'=>'暂未签收']);  
			 }
		}else{
			return json(['code'=>404,'msg'=>'请求方式错误']);
		}
	}
	// 归还订单
	public function getstate(){
			$userid= request()->param('userid');
			$platform_id= request()->param('platform_id');
			if($platform_id!=2){
				$data=Db::name('orderlist')
				->alias('a')
				->join('schoolproduct b','b.id=a.product_id')
				->join('product c','c.id=b.product_id')
				->join('useraddress d','d.id=a.address_id')
				->where(['a.user_id'=>$userid,'a.paystatus'=>1,'a.deliver_status'=>1,'a.state'=>3])
				->where('a.platform_id','in',[2,$platform_id])
				->order('a.create_time desc')
				->field('a.*,c.name,c.image,d.detail as address')
				->select()
				->toArray();
			}else{
				$data=Db::name('orderlist')
				->alias('a')
				->join('schoolproduct b','b.id=a.product_id')
				->join('product c','c.id=b.product_id')
				->join('useraddress d','d.id=a.address_id')
				->where(['a.user_id'=>$userid,'a.paystatus'=>1,'a.deliver_status'=>1,'a.state'=>3,'a.platform_id'=>2])
				->order('a.create_time desc')
				->field('a.*,c.name,c.image,d.detail as address')
				->select()
				->toArray();
			}
			
			foreach($data as $k=>$v){
				if (!isHttpsUrlByRegex($v['image'])) {
				    $data[$k]['image']=imgurl($v['image']);
				}
					
			} 
			return json(['code'=>200,'msg'=>'获取成功','data'=>$data]); 
	}
}